package cn.factorybox.item.controller;

import cn.factorybox.common.entity.Result;
import cn.factorybox.item.annotation.ControllerLogAnnotation;
import cn.factorybox.item.service.FactoryDeviceService;
import cn.factorybox.item.service.UserService;
import cn.factorybox.model.entity.FactoryDevice;
import cn.factorybox.model.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-09-11 11:17
 */
@Api(tags = "工厂终端设备管理")
@RestController
public class FactoryDeviceController {

    @Autowired
    private FactoryDeviceService factoryDeviceService;

    @Autowired
    private UserService userService;

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "新增设备")
    @ApiOperation(value = "新增设备", notes = "新增设备")
    @PostMapping("/factoryDevice")
    public Result saveFactoryDevice(@RequestBody FactoryDevice factoryDevice) {
        factoryDeviceService.save(factoryDevice);
        return Result.ok("新建设备成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "编辑设备")
    @ApiOperation(value = "编辑设备", notes = "编辑设备")
    @PutMapping("/factoryDevice")
    public Result updateFactoryDevice(@RequestBody FactoryDevice factoryDevice) {
        factoryDeviceService.update(factoryDevice);
        return Result.ok("修改设备信息成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "根据id获取设备信息")
    @ApiOperation(value = "根据id获取设备信息", notes = "根据id获取设备信息")
    @GetMapping("/factoryDevice")
    public Result getFactoryDevice(@RequestParam("id") String id) {
        FactoryDevice factoryDevice = factoryDeviceService.getFactoryDevice(id);
        return new Result(factoryDevice,"修改设备信息成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "删除设备")
    @ApiOperation(value = "删除设备", notes = "删除设备")
    @DeleteMapping("/factoryDevice")
    public Result deleteFactoryDevice(@RequestParam("id") String id) {
        factoryDeviceService.delete(id);
        return Result.ok("删除设备成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取设备信息 - 简短版")
    @ApiOperation(value = "获取设备信息 - 简短版", notes = "获取设备信息 - 简短版")
    @GetMapping("/factoryDevice/shortlist")
    public Result selectListByRoomId(@RequestParam("roomId") String roomId) {
        List<FactoryDevice> deviceList = factoryDeviceService.selectListByRoomId(roomId);
        return new Result(deviceList,"数据获取成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取设备列表")
    @ApiOperation(value = "获取设备列表", notes = "获取设备列表")
    @GetMapping("/factoryDevice/list")
    public Result selectList(@RequestParam("number") Integer number) {
        Map<String,Object> data =  factoryDeviceService.selectList(number);
        return Result.ok(data,"获取设备列表成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取所有设备列表")
    @ApiOperation(value = "获取所有设备列表", notes = "获取所有设备列表")
    @GetMapping("/factoryDevice/all")
    public Result selectList() {
        Map<String,Object> data =  factoryDeviceService.selectAllDevices();
        return Result.ok(data,"获取设备列表成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取设备分布信息")
    @ApiOperation(value = "获取设备分布信息", notes = "获取设备分布信息")
    @GetMapping("/factoryDevice/distribute")
    public Result getDistributeMessage() {
        Map<String,Object> map = factoryDeviceService.getDistributeMessage();
        return Result.ok(map,"获取设备分布信息成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取所有设备信息 - 正常版")
    @ApiOperation(value = "获取所有设备信息 - 正常版", notes = "获取所有设备信息 - 正常版")
    @GetMapping("/factoryDevice/alldevices")
    public Result selectAllDevices(HttpServletRequest request) throws Exception {
        List<FactoryDevice> devices = factoryDeviceService.selectAllDevicesOfVersion3(request);
        return new Result(devices,"数据获取成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取单个设备的数据总览")
    @ApiOperation(value = "获取单个设备的数据总览", notes = "获取单个设备的数据总览")
    @GetMapping("/factoryDevice/single")
    public Result getSingleDeviceData(@RequestParam("id") String id) {
        return new Result(factoryDeviceService.getSingleDeviceData(id),"返回数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取设备最新展示数据")
    @ApiOperation(value = "获取设备最新展示数据", notes = "获取设备最新展示数据")
    @GetMapping("/factoryDevice/lastest")
    public Result getSingleDeviceLatestData(@RequestParam("id") String id) {
        return new Result(factoryDeviceService.getSingleDeviceLatestData(id),"返回数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取设备前一周的总数据量")
    @ApiOperation(value = "获取设备前一周的总数据量", notes = "获取设备前一周的总数据量")
    @GetMapping("/factoryDevice/week")
    public Result selectDataBeforeAweek(@RequestParam("id") String id) {
        return new Result(factoryDeviceService.countAllBeforeAweek(id),"返回数据成功");
    }

    @ControllerLogAnnotation(moduleName = "工厂终端设备管理", functionName = "获取主管下设备，用于上位机登录获取设备号")
    @ApiOperation(value = "获取主管下设备", notes = "获取主管下设备")
    @PostMapping("/factoryDevice/login")
    public Result terminalLogin(@RequestBody Map<String,Object> map) {
        // 先进行登录验证，这里的传参使用json不是表单，因为上位机传表单暂时有问题，故使用json
        User userInfo = userService.selectUserWithoutIp(map);
        if (userInfo == null) {
            // 登录失败
            return Result.error("登录失败");
        }
        Map<String,Object> result = factoryDeviceService.selectAllDevicesOfCompany(userInfo.getCompanyId());
        return Result.ok(result);
    }

}
